<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PartyController
 *
 * @author Poh Nean
 */
class PartyController extends PsController
{
	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
					)), parent::accessRules());
	}

	public function filters()
	{
		return CMap::mergeArray(parent::filters(), array(
					'ajaxOnly 
						+ autocomplete'
				));
	}

	/**
	 * Find all action. Used by ajax function in select clients to retrieve 
	 * a full list of contacts of the agent.
	 * @param type $term 
	 */
	public function actionAutocomplete($term)
	{
		$validColumns = array('email');

		if (strlen($term) > 0) {
			$data = array();

			$contactCriteria = new CDbCriteria();
			$contactCriteria->compare('t.name', $term, true);
			$contactCriteria->order = 'name ASC';
			$contactCriteria->limit = 10;
			$contacts = Contact::model()->ofCurrentAgency()->notRemoved()->findAll($contactCriteria);
			if (!empty($contacts)) {
				foreach ($contacts as $contact) {
					$name = $contact->name;
					$id = $contact->id;

					$tmp = array(
						'id' => $id,
						'label' => $name,
						'party_id' => $contact->party_id,
						'address' => str_replace(",", "\n", $contact->address_formatted),
						'postcode' => $contact->postcode,
						'building' => $contact->building,
						'block' => $contact->block,
						'street' => $contact->street,
						'floor' => $contact->floor,
						'unit' => $contact->unit,
						'country_code' => $contact->country_code,
						'category' => 'Contacts',
					);

					$data[] = $tmp;
				}
			}



//			$userCriteria = new CDbCriteria();
//			$userCriteria->compare('t.name', $term, true);
//			$userCriteria->order = 'name ASC';
//			$userCriteria->limit = 10;
//
//			$internalAgents = User::model()->ofCurrentAgency()->findAll($userCriteria);
//
//			if (!empty($internalAgents)) {
//				foreach ($internalAgents as $agent) {
//					$name = $agent->name;
//					$id = $agent->id;
//
//					$tmp = array(
//						'id' => $id,
//						'label' => $name,
//						'party_id' => $agent->party_id,
//						'address' => str_replace(",", "\n", $agent->address_formatted),
//						'category' => Yii::app()->user->model->agency->name . ' Agents',
//					);
//
//					$data[] = $tmp;
//				}
//			}
//			
//			$agencyCriteria = new CDbCriteria();
//			$agencyCriteria->compare('t.name', $term, true);
//			$agencyCriteria->order = 'name ASC';
//			$agencyCriteria->limit = 10;
//			$agencies = Agency::model()->findAll($agencyCriteria);
//
//			if (!empty($agencies)) {
//				foreach ($agencies as $agency) {
//					$name = $agency->name;
//					$id = $agency->id;
//
//					$tmp = array(
//						'id' => $id,
//						'label' => $name,
//						'party_id' => $agency->party_id,
//						'address' => str_replace(",", "\n", $agency->address_formatted),
//						'category' => 'Agencies',
//					);
//
//					$data[] = $tmp;
//				}
//			}

			print json_encode($data);
		}
	}

}

?>
